<?php
namespace app\admin\controller;

use app\BaseController;
use app\middleware\AdminCheck;
use think\facade\Db;

class Index extends BaseController
{

    protected $middleware = [
        AdminCheck::class => ['except' 	=> ['cityStatics'] ]
    ];



    /**
    *  @SWG\Get(
    *    path="/admin/index/numberStatistics",
    *    tags={"后台首页统计相关"},
    *    summary="顶部数据统计",
    *    @SWG\Parameter(
    *    in="query",
    *    required=false,
    *    name="id",
    *    type="integer",
    *    description="用户的id"),
    *  @SWG\Response(response="200",description="ok")
    *  )
    */
    /**
     * 顶部数据统计
     * @return \think\response\Json
     */
//    public function numberStatistics(){
//        //获取今天的时间
//        $today_start=date('Y-m-d 00:00:01');
//        $today_end=date('Y-m-d 23:59:59');
//        //获取昨天的时间
//        $yesterday_start=makeDay($today_start,'-1 day');
//        $yesterday_end=makeDay($today_end,'-1 day');
//
//        //今日注册用户量
//        $today_user_number=Db::name('user')
//            ->where('is_deleted',0)
//            ->where('create_at','between',[$today_start,$today_end])
//            ->count();
//        //昨日注册用户量
//        $yesterday_user_number=Db::name('user')
//            ->where('is_deleted',0)
//            ->where('create_at','between',[$yesterday_start,$yesterday_end])
//            ->count();
//        //用户日环比增长
//        if($yesterday_user_number != 0 ) {
//            $day_user_rate = round(($today_user_number - $yesterday_user_number) / $yesterday_user_number, 4);
//        }else{
//            $day_user_rate = 0;
//        }
//        //今日考试表单量
//        $today_test_sign_up=Db::name('sign_up_form')
//            ->where('is_deleted',0)
//            ->where('create_at','between',[$today_start,$today_end])
//            ->count();
//        $yesterday_test_sign_up=Db::name('sign_up_form')
//            ->where('is_deleted',0)
//            ->where('create_at','between',[$yesterday_start,$yesterday_end])
//            ->count();
//        //考试报名表单日环比增长
//        if($yesterday_test_sign_up != 0) {
//            $day_test_sign_up_rate = round(($today_test_sign_up - $yesterday_test_sign_up) / $yesterday_test_sign_up, 4);
//        }else{
//            $day_test_sign_up_rate = 0;
//        }
//
//        //考题城市数量
//        $cityCount = Db::name('chapter')
//            ->group('city_')
//            ->count();
//
//        //抖音用户的数量和微信的用户数量
//        $wx_user_count = Db::name('user')
//            ->where('platform','wx')
//            ->where('is_deleted',0)
//            ->count();
//        $dy_user_count = Db::name('user')
//            ->where('platform','dy')
//            ->where('is_deleted',0)
//            ->count();
//
//        $data['user'] = [
//            'today_user_number'=>$today_user_number,
//            'yesterday_user_number'=>$yesterday_user_number,
//            'day_user_rate'=>$day_user_rate
//        ];
//        $data['user_distribution'] = [
//            'wx_user_count'=>$wx_user_count,
//            'dy_user_number'=>$dy_user_count,
//        ];
//        $data['test_sign_up']=[
//            'today_test_sign_up'=>$today_test_sign_up,
//            'yesterday_test_sign_up'=>$yesterday_test_sign_up,
//            'day_test_sign_up_rate'=>$day_test_sign_up_rate
//        ];
//        $data['city']=[
//            'cityCount'=>$cityCount,
//        ];
//
//        return success('今日数据统计',$data);
//    }
    public function numberStatistics(){
        //获取今天的时间
        $today_start=date('Y-m-d 00:00:01');
        $today_end=date('Y-m-d 23:59:59');
        //获取昨天的时间
        $yesterday_start=makeDay($today_start,'-1 day');
        $yesterday_end=makeDay($today_end,'-1 day');

        $uncomplateNum = Db::name('pay_order')
            ->whereIn('status', [10, 11, 20, 30, 40])
            ->count();
        $complateNum = Db::name('pay_order')
            ->whereIn('status', [50])
            ->count();

        return success('今日数据统计', [
            'uncomplate_num' => $uncomplateNum,
            'complate_num' => $complateNum,
            'total' => $uncomplateNum + $complateNum,
        ]);
    }


    /**
    *   @SWG\Post(
    *     path="/admin/index/userStatics",
    *     tags={"后台首页统计相关"},
    *     summary="平台用户注册统计",
    *     consumes={"application/json"},
    *     produces={"application/json"},
    *     @SWG\Parameter(
    *      in="query",
    *      required=false,
    *      name="start_time,end_time",
    *      type="string",
    *      description="start_time(开始时间),end_time（结束时间）"),
    *     @SWG\Response(response="200",description="ok")
    *  )
    */
    /**
     * 平台用户注册统计
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function userStatics(){
        validateParam('start_time,end_time','start_time,end_time');
        $param=input();
        //获取日期
        $start_time=date('Y-m-d 00:00:00',strtotime($param['start_time']));
        $end_time=date('Y-m-d 23:59:59',strtotime($param['end_time']));
        //男性数量统计
        $list1 = Db::name('user')
            ->field('DATE_FORMAT(create_at, "%Y-%m-%d") as label,count(*) as value1')
            ->where('create_at','>=',$start_time)
            ->where('create_at','<=',$end_time)
            ->group('label')
            ->select()->toArray();
        $list1_temp=[];
        foreach ($list1 as $k=>$v){
            $list1_temp[$v['label']] = $v['value1'];
        }


        $diff = (strtotime($end_time) - strtotime($start_time))/(3600*24);
        $date = [];
        for ($i=0;$i<=$diff;$i++){
            $date[$i]['label']=date('Y-m-d',strtotime("+{$i} days",strtotime($start_time)));
            $date[$i]['value']=isset($list1_temp[$date[$i]['label']]) ? $list1_temp[$date[$i]['label']] : 0;
        }
        return success('平台用户注册统计',$date);
    }



    /**
    *  @SWG\Get(
    *    path="/admin/index/cityStatics",
    *    tags={"后台首页统计相关"},
    *    summary="城市题库数量统计",
    *    @SWG\Parameter(
    *    in="query",
    *    required=false,
    *    name="id",
    *    type="integer",
    *    description="不需要参数"),
    *  @SWG\Response(response="200",description="ok")
    *  )
    */
    /**
     * 城市统计
     * @return \think\response\Json
     */
    public function cityStatics(){
        //查询拥有考题的城市有多少
        $sql = "SELECT city_ as city,SUM(total_) as total,COUNT(id_) as chapter_count FROM chapter GROUP BY city_";
        $city = Db::query($sql);
        foreach ($city as $k=>$v){
            if($v['city'] === '0'){
                $city[$k]['city'] = '全国';
            }
        }
        return success('城市题库数量和章节统计',$city);
    }



    /**
     * 订单统计
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderStatics(){
        validateParam('start_time,end_time','start_time,end_time');
        $status = [10, 11, 20, 30, 40, 50];
        if (input('is_complated')) {
            $status = [50];
        }
        $param=input();
        //获取日期
        $start_time=date('Y-m-d 00:00:00',strtotime($param['start_time']));
        $end_time=date('Y-m-d 23:59:59',strtotime($param['end_time']));
        $list1 = Db::name('pay_order')
            ->field('DATE_FORMAT(create_at, "%Y-%m-%d") as label,sum(pay_money) as value1,count(*) as value2')
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->where('is_pay','=',1)
            ->whereIn('status', $status)
            ->group('label')
            ->select()->toArray();
        $list1_temp=[];
        foreach ($list1 as $k=>$v){
            $list1_temp[$v['label']] = [$v['value1'],$v['value2']];
        }
        //获取未支付订单数量
//        $list2 = Db::name('pay_order')
//            ->field('DATE_FORMAT(create_at, "%Y-%m-%d") as label,count(*) as value3')
//            ->where('create_at','>=',$start_time)
//            ->where('create_at','<=',$end_time)
//            ->where('is_pay','=',0)
//            ->group('label')
//            ->select()->toArray();
//        $list2_temp=[];
//        foreach ($list2 as $k=>$v){
//            $list2_temp[$v['label']] = $v['value3'];
//        }
        $diff = (strtotime($end_time) - strtotime($start_time))/(3600*24);
        $date = [];
        for ($i=0;$i<=$diff;$i++){
            $date[$i]['label']=date('Y-m-d',strtotime("+{$i} days",strtotime($start_time)));
            $date[$i]['value1']=isset($list1_temp[$date[$i]['label']]) ? $list1_temp[$date[$i]['label']][0] : 0;
            $date[$i]['value2']=isset($list1_temp[$date[$i]['label']]) ? $list1_temp[$date[$i]['label']][1] : 0;
//            $date[$i]['value3']=isset($list2_temp[$date[$i]['label']]) ? $list2_temp[$date[$i]['label']] : 0;
        }
//        //获取消费的总金额Gmv
//        $total_money = Db::name('pay_order')
//            ->where('create_at','>=',$start_time)
//            ->where('create_at','<=',$end_time)
//            ->where('is_pay','=',1)
//            ->sum('pay_money');
//        //订单量(已支付的订单量)
//        $total_count = Db::name('pay_order')
//            ->where('create_at','>=',$start_time)
//            ->where('create_at','<=',$end_time)
//            ->where('is_pay','=',1)
//            ->count();
//        //未支付订单量
//        $not_pay_count = Db::name('pay_order')
//            ->where('create_at','>=',$start_time)
//            ->where('create_at','<=',$end_time)
//            ->where('is_pay','=',0)
//            ->count();
        return success('订单统计',['date'=>$date]);
    }






}
